package com.zl.data.provider;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.zl.data.config.DataConstant;
import com.zl.data.db.DataBeanInfo;
import com.zl.data.db.DataOpenHelper;

/**
 * @author ZL @朱林</a>
 * @Version 1.0
 * @Description TODO
 * @date 2021/01/08  15:00
 */
public class DataProvider extends ContentProvider {
    private static final String TAG="DataProvider";
    private DataOpenHelper dataOpenHelper;
    private static final UriMatcher MATCHER;
    private final Object mLock = new Object();
    private static final int DATA_STORE_INFO_CODE = 1;

    static {
        MATCHER = new UriMatcher(UriMatcher.NO_MATCH);
        MATCHER.addURI(DataConstant.DATA_STORE_AUTHORITY, DataBeanInfo.TABLE_NAME, DATA_STORE_INFO_CODE);
    }

    @Override
    public boolean onCreate() {
        this.dataOpenHelper=new DataOpenHelper(getContext());
        return true;
    }

    @Nullable
    @Override
    public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String sortOrder) {
        try {
            synchronized (mLock) {
                SQLiteDatabase db = dataOpenHelper.getReadableDatabase();
                switch (MATCHER.match(uri)) {
                    case DATA_STORE_INFO_CODE:
                        return db.query(DataBeanInfo.TABLE_NAME, projection, selection, selectionArgs, null, null, sortOrder);
                    default:
                        throw new IllegalArgumentException("Unknown Uri:" + uri.toString());
                }
            }
        } catch (SQLException e) {
            Log.e(TAG, "query error:" + e.getMessage());
            return null;
        }
    }

    @Nullable
    @Override
    public String getType(@NonNull Uri uri) {
        return null;
    }

    @Nullable
    @Override
    public Uri insert(@NonNull Uri uri, @Nullable ContentValues values) {
        SQLiteDatabase db = dataOpenHelper.getWritableDatabase();
        switch (MATCHER.match(uri)) {
            case DATA_STORE_INFO_CODE:
                long rowId = db.insert(DataBeanInfo.TABLE_NAME, null, values);
                Uri insertUri = ContentUris.withAppendedId(uri, rowId);
                this.getContext().getContentResolver().notifyChange(uri, null);
                return insertUri;
            default:
                throw new IllegalArgumentException("Unknown Uri:" + uri.toString());
        }
    }

    @Override
    public int delete(@NonNull Uri uri, @Nullable String selection, @Nullable String[] selectionArgs) {
        SQLiteDatabase db = dataOpenHelper.getWritableDatabase();
        switch (MATCHER.match(uri)) {
            case DATA_STORE_INFO_CODE:
                int  count = db.delete(DataBeanInfo.TABLE_NAME, selection, selectionArgs);
                return count;
            default:
                throw new IllegalArgumentException("Unknown Uri:" + uri.toString());
        }
    }

    @Override
    public int update(@NonNull Uri uri, @Nullable ContentValues values, @Nullable String selection, @Nullable String[] selectionArgs) {
        SQLiteDatabase db = dataOpenHelper.getWritableDatabase();
        switch (MATCHER.match(uri)) {
            case DATA_STORE_INFO_CODE:
                int count = db.update(DataBeanInfo.TABLE_NAME, values, selection, selectionArgs);
                return count;
            default:
                throw new IllegalArgumentException("Unknown Uri:" + uri.toString());
        }
    }
}
